<template>
  <view class="login-layout">
    <TopBar></TopBar>
    <view class="center">
      <image src="/static/default-avatar.png" mode="aspectFill"></image>
      <text>停车系统</text>
    </view>
    <view class="bottom">
      <button
        type="default"
        class="bottom-btn"
        open-type="getPhoneNumber"
        @getphonenumber="onGetPhoneNumber"
      >
        登录
      </button>
      <view class="content">
        <label @click="toggleAgree">
          <checkbox color="#0680FE" :checked="isAgree"/>
          <text>我已阅读并同意</text>
          <navigator url="" style="color: #0680fe; display: inline-block"
            >《隐私政策》</navigator
          >
        </label>
      </view>
    </view>
  </view>
</template>

<script setup>
import TopBar from "../../component/topbar.vue"; // 导入子组件
import { ref } from "vue";
//控制勾选协议，默认不勾选
const isAgree = ref(false)
//切换协议的变化
const toggleAgree = ()=>{
	isAgree.value = !isAgree.value
}
const onGetPhoneNumber = (e) => {
  console.log("onGetPhoneNumber触发", e);
  console.log("getPhoneNumber detail:", e.detail);
  //勾选协议
if(!isAgree.value){
	uni.showToast({
		title:'请勾选协议',
		icon:'none'
	})
	return
}
  // 授权拒绝
  if (e.detail.errMsg !== "getPhoneNumber:ok") {

    uni.showToast({
      title: "您拒绝了授权",
      icon: "none",
    });
    return;
  }

  uni.login({
    provider: "weixin",
    success: (loginRes) => {
      console.log("微信登录", loginRes.code);
      const code = loginRes.code; // 保存 code
      uni.request({
        url: "https://qwscrm.com:9202/login/v1/user/wx",
        method: "POST",
        data: {
          code,
        },
        success: (wxRes) => {
          console.log("后端登录返回:", wxRes);

          if (wxRes.data.code === 0) {
            const { token, openId, userId } = wxRes.data.data;
            const appInfo = uni.getAccountInfoSync();
            const appId = appInfo.miniProgram.appId;

            // 存储 token 和 openid
            uni.setStorageSync("token", token);
            uni.setStorageSync("openid", openId);
            uni.setStorageSync("userid",userId)
            //  调用获取手机号接口
            uni.request({
              url: "https://qwscrm.com:9202/login/v1/user/wx-phone",
              method: "POST",
              header: {
                Authorization: `Bearer ${token}`, // 把 token 放进去
              },
              data: { code: e.detail.code, appId: appId, userId: userId },
              success: (phoneRes) => {
                console.log("手机号接口返回", phoneRes);
				console.log(phoneRes.data.data.phone);
				uni.setStorageSync('phone',phoneRes.data.data.phone)
				uni.reLaunch({
					url:'/pages/index/index',
				})
              },
              fail: (err) => {
                console.error("手机号请求失败", err);
              },
            });
          }
        },
      });
    },
  });
};
</script>

<style lang="less">
.login-layout {
  .center {
    margin-top: 80pt;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    image {
      width: 80pt;
      height: 80pt;
    }
    text {
      width: 60pt;
      height: 15pt;
      text-align: left;
      color: #333;
      font-size: 15pt;
      line-height: 15pt;
      margin-top: 16pt;
      font-weight: 500;
      font-family: "Source Han Sans CN-Medium";
    }
  }
  .bottom {
    .bottom-btn {
      height: 40pt;
      margin: 0 40pt;
      background-color: #0680fe;
      border-radius: 30pt;
      text-align: center;
      line-height: 40pt;
      margin-top: 88pt;
      font-size: 15pt;
      color: #ffffff;
    }
    .content {
      display: flex;
      margin-top: 32pt;
      margin-left: 56pt;
    }
  }
}
</style>